/*
 * @Description: 购物车状态模块
 * @Author: hai-27
 * @Date: 2020-02-21 18:40:41
 * @LastEditors: hai-27
 * @LastEditTime: 2020-03-07 20:38:55
 */
export default {
    state: {
        shoppingCart: [
            {
                id: "1", // 购物车id
                productID: "1", // 商品id
                productName: "商品名称111", // 商品名称
                productImg: "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/18d2099cb0b05bbd23cb1915dfc9d0d6.jpg", // 商品图片
                price: 388, // 商品价格
                num: 5, // 加购的商品数量
                maxNum: 9, // 商品限购数量
                check: true // 是否勾选
            },
            {
                id: "2", // 购物车id
                productID: "2", // 商品id
                productName: "商品名称222", // 商品名称
                productImg: "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/28febc579eaea6b07c96c0ec48b89f7a.jpg", // 商品图片
                price: 299, // 商品价格
                num: 2, // 加购的商品数量
                maxNum: 999, // 商品限购数量
                check: false // 是否勾选
            },
        ]
    },
    getters: {//获取state数据
        getShoppingCart(state) {
            // 获取购物车状态
            return state.shoppingCart;
        },
        getNum(state) {
            // 购物车商品总数量
            let totalNum = 0;
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];

                totalNum += temp.num;
            }
            return totalNum;
        },
        getIsAllCheck(state) {
            // 判断是否全选
            let isAllCheck = true;
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                // 只要有一个商品没有勾选立即return false;
                if (!temp.check) {
                    isAllCheck = false;
                    return isAllCheck;
                }
            }
            return isAllCheck;
        },
        getCheckGoods(state) {
            // 获取勾选的商品信息
            // 用于确认订单页面
            let checkGoods = [];
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                if (temp.check) {
                    checkGoods.push(temp);
                }
            }
            return checkGoods;
        },
        getCheckNum(state) {
            // 获取购物车勾选的商品数量
            let totalNum = 0;
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                if (temp.check) {
                    totalNum += temp.num;
                }
            }
            return totalNum;
        },
        getTotalPrice(state) {
            // 购物车勾选的商品总价格
            let totalPrice = 0;
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                if (temp.check) {
                    totalPrice += temp.price * temp.num;
                }
            }
            return totalPrice;
        }
    },
    mutations: {
        setShoppingCart(state, data) {
            // 设置购物车状态
            state.shoppingCart = data;//data必须是一个数组
        },
        unshiftShoppingCart(state, data) {
            // 添加购物车
            // 用于在商品详情页点击添加购物车,后台添加成功后，更新vuex状态
            state.shoppingCart.unshift(data);
        },
        updateShoppingCart(state, payload) {
            // 更新购物车
            // 更新商品数量 & 设置勾选
            if (payload.prop == "num") {
                // 判断商品数量是否大于限购数量或小于1
                if (state.shoppingCart[payload.key].maxNum < payload.val) {
                    return;
                }
                if (payload.val < 1) {
                    return;
                }

            }

            // 根据商品在shoppingCart数组的索引和属性更改--更新
            //check 勾选状态
            //num 更新
            state.shoppingCart[payload.key][payload.prop] = payload.val;
        },
        addShoppingCartNum(state, productID) {
            // 增加购物车商品数量
            // 用于在商品详情页点击添加购物车,后台返回002，“该商品已在购物车，数量 +1”，更新vuex的商品数量
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                if (temp.productID == productID) {
                    if (temp.num < temp.maxNum) {
                        temp.num++;
                    }
                }
            }
        },
        deleteShoppingCart(state, id) {
            // 根据 商品id 删除购物车商品
            for (let i = 0; i < state.shoppingCart.length; i++) {
                const temp = state.shoppingCart[i];
                if (temp.productID == id) {
                    state.shoppingCart.splice(i, 1);
                }
            }
        },
        checkAll(state, data) {
            // 点击全选按钮，更改每个商品的勾选状态
            for (let i = 0; i < state.shoppingCart.length; i++) {
                state.shoppingCart[i].check = data;
            }
        }
    },
    actions: {
        // 初始化购物车
        setShoppingCart({commit}, data) {
            commit('setShoppingCart', data);
        },
        // 新加入购物车
        unshiftShoppingCart({commit}, data) {
            commit('unshiftShoppingCart', data);
        },
        // 修改购物车
        updateShoppingCart({commit}, payload) {
            commit('updateShoppingCart', payload);
        },
        // 购物车数量+1
        addShoppingCartNum({commit}, productID) {
            commit('addShoppingCartNum', productID);
        },
        // 删除购物车
        deleteShoppingCart({commit}, id) {
            commit('deleteShoppingCart', id);
        },
        // 全选
        checkAll({commit}, data) {
            commit('checkAll', data);
        }
    }
}
